/*********************************************************************\
*
*                  (c) lkc
*                  All Rights Reserved. lkc Confidential
*
\*********************************************************************/
/*********************************************************************\
*   Title                      :
*
*   Module Description         :
*
*   Author                     : lkc
*
*   Created                    : 2018-6-24
*
\*********************************************************************/
#include <string.h>
//#include <chrono>
#include <fcntl.h>      /*文件控制定义*/
#include "UtilityTH.h"

//自定义打印
//#define DEBUG_PRINTF
void DebugPrintf(const char *cmd,...)
{
    #ifdef DEBUG_PRINTF
//    printf("%s %s ", __DATE__, __TIME__);
    va_list args;       //定义一个va_list类型的变量，用来储存单个参数
    va_start(args,cmd); //使args指向可变参数的第一个参数
    vprintf(cmd,args);  //必须用vprintf等带V的
    va_end(args);       //结束可变参数的获取
//    printf("\n");
    #else
    #endif
}


void MyWrite(const char* fileName,const char* modes, const char* content)
{
    FILE *fp = fopen(fileName,modes);
    if(fp == nullptr)
    {
        printf("%s does not exist\n",fileName);
        return;
    }
    fwrite(content,strlen(content),1,fp);
    DebugPrintf("%s exists and write %s\n",fileName,content);
    fclose(fp);
}

int SetNonblocking(int fd)//设置为非阻塞状态
{
    int flags = fcntl( fd, F_GETFL );
    flags |= O_NONBLOCK;
    if(fcntl( fd, F_SETFL, flags )==-1)
    {
        printf("fcntl failed!\n");
        return -1;
    }
    return 0;
}

int SetBlocking(int fd)//设置为阻塞状态
{
    int flags = fcntl( fd, F_GETFL );
    flags &= ~O_NONBLOCK;
    if(fcntl(fd,F_SETFL,flags) ==-1)
    {
        printf("fcntl failed!\n");
        return -1;
    }
    return 0;
}

unsigned char CheckSum(const unsigned char* start,int count)
{
    unsigned char checksum = 0;
    for (int i = 0; i < count; i++)
    {
        checksum += start[i];
    }
    return checksum;
}

//unsigned char checkSum(const std::vector<unsigned char> &vec,int start,int end)
//{
//    unsigned char checksum = 0;
////    vector<unsigned char>::const_iterator it = vec.begin();
//    for (auto iter = vec.begin()+start; iter != vec.begin()+start+end; iter++)
//    {
//        checksum += (*iter);
//    }
//    return checksum;
//}

unsigned char CheckSum(std::deque<unsigned char>::const_iterator start,int count)
{
    unsigned char checksum = 0;
    for (int i = 0; i < count; i++)
    {
        checksum += (*(start+i));
//        checksum +=  start[i];
    }
    return checksum;
}

QImage MatToQImage(const cv::Mat &mat)
{
    if(mat.type()==CV_8UC1)
    {
        return QImage((const unsigned char*)(mat.data), mat.cols,mat.rows, mat.step, QImage::Format_Grayscale8);//Format_Grayscale8这种方式更快,耗时30us,下面的方式耗时1ms 如果使用的是5.5之前的Qt，因为还没有实现Format_Grayscale8这个类型，处理单通道灰度图会复杂一些，
//        auto start = std::chrono::system_clock::now();
//        // Set the color table (used to translate colour indexes to qRgb values)
//        QVector<QRgb> colorTable;
//        for (int i=0; i<256; i++)
//        {
//            colorTable.push_back(qRgb(i,i,i));
//        }
//        // Copy input Mat
//        const uchar *qImageBuffer = (const uchar*)mat.data;
//        // Create QImage with same dimensions as input Mat
//        QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
//        img.setColorTable(colorTable);
//        auto end = std::chrono::system_clock::now();
//        auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
//        qDebug() <<"gryMat To Image " << static_cast<double>(duration.count())<< " us\n";
//        return img;
    }
    else if(mat.type()==CV_8UC3)
    {
//        return QImage((const unsigned char*)(mat.data),mat.cols,mat.rows, mat.step, QImage::Format_RGB888);//RGB顺序不对的,耗时50us
        //RGB顺序正确,耗时4ms
        static cv::Mat rgb;

        cv::cvtColor(mat,rgb,cv::COLOR_BGR2RGB);
//        cv::cvtColor(mat,rgb,CV_BGR2RGB);
        return QImage((const unsigned char*)(rgb.data),rgb.cols,rgb.rows, rgb.step, QImage::Format_RGB888);
    }
//    else if(mat.type() == CV_8UC4)
//    {
//        // Copy input Mat
//        const uchar *pSrc = (const uchar*)mat.data;
//        // Create QImage with same dimensions as input Mat
//        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
//        return image.copy();
//    }
    else
    {
        qDebug() << "ERROR: Mat could not be converted to QImage.";
        return QImage();
    }
}


/********************************************************************\
 *
 * REVISION RECORDS
 *
\*********************************************************************/
/*********************************************************************/
/*
 *
 *
 *
\*********************************************************************/
/*------------------------------The End------------------------------*/

